#include <iostream>

using namespace std;
int a[14][5]; 
int cnt[14];

void dfs(int b)
{
	if (b == 13) return;
	
	b = a[b][cnt[b]--];
	dfs(b);
}

int main()
{
	for (int i = 1; i <= 13; i++)
	{
		for (int j = 1; j <= 4; j++)
		{
			char ch; cin >> ch;
			if (ch >= '1' && ch <= '9') 
			{
				a[i][j] = ch -'0';
			}
			else if (ch == 'A') a[i][j] = 1;
			else if (ch == 'J') a[i][j] = 11;
			else if (ch == 'Q') a[i][j] = 12;
			else if (ch == 'K') a[i][j] = 13;
			else a[i][j] = 10;
		}
	}
	
	for (int i = 1; i <= 13; i++) cnt[i] = 4;
	
	for (int i = 1; i <= 4; i++)	
	{
		dfs(a[13][i]);
	}
	
	int ret = 0;
	for (int i = 1; i <= 13; i++)
	{
		if (cnt[i] == 0) ret++;
	} 
	
	cout << ret << endl;
	
	return 0;
}
